<template>
  <!-- 维护作业-VIP件维护页面 -->
  <div class="fillcontain">
    <el-form :model="searchParams" ref="ruleForm" label-width="100px" class="search_container">
      <el-row>
        <el-col :span="6">
          <el-form-item label="公司" prop="chdrcoy">
            <el-input clearable v-model="searchParams.chdrcoy" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6" class="text_center">
          <el-button type="primary" icon="el-icon-search" @click="subGetList"></el-button>
          <el-button type="primary" plain icon="el-icon-refresh" @click="resetSearch"></el-button>
        </el-col>
      </el-row>
    </el-form>
    <div class="contain">
      <el-row class="bgfff pb15">
        <el-col :span="24" class="pt5">
          <el-button @click="addVipInfo" type="danger" plain>新增</el-button>
        </el-col>
      </el-row>
      <!-- 查询结果 -->
      <div class="table_container">
        <el-table
            v-loading="loading"
            :data="tableData"
            highlight-current-row
            header-cell-class-name="table-header-class"
            style="width:100%">
          <el-table-column
              min-width="120"
              property="chdrcoy"
              label="公司"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="nannprem"
              label="年缴化保费"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="sumins"
              label="寿险保额"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="120"
              property="branch"
              label="Branch"
              align='center'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              fixed="right"
              label="操作"
              align='center'>
            <template slot-scope="scope">
              <!-- <el-button type="text" size="small" @click="modifyVipInfo(scope.row)">修改</el-button> -->
              <span class="operationIcon" @click="modifyVipInfo(scope.row)">
                              <el-tooltip class="item" effect="light" content="修改" placement="top">
                                <i class="el-icon-edit"></i>
                              </el-tooltip>
                          </span>
              <!--  <el-button type="text" size="small" @click="deleteVipInfo(scope.row)">删除</el-button> -->
              <span class="operationIcon ml5" @click="deleteVipInfo(scope.row)">
                                <el-tooltip class="item" effect="light" content="删除" placement="top">
                                  <i class="el-icon-delete"></i>
                                </el-tooltip>
                            </span>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-col :span="24">
            <div class="pagination">
              <el-pagination
                  background
                  v-if='paginations.total > 0'
                  :page-sizes="paginations.pageSizes"
                  :page-size="paginations.pageSize"
                  :layout="paginations.layout"
                  :total="paginations.total"
                  :current-page='paginations.pageIndex'
                  @current-change='handleCurrentChange'
                  @size-change='handleSizeChange'>
              </el-pagination>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <!-- 弹框 修改-->
    <el-dialog
        title="VIP件维护"
        :visible.sync="showVIpDialog"
        width="50%"
        center>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12" class="common-box">
            <el-form-item label="公司" prop="chdrcoy">
              <el-input v-model="form.chdrcoy" placeholder="请输入" maxlength='1'
                        :readonly="form.method == 'modify' ? true : false"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="年缴化保费" prop="nannprem">
              <el-input-number v-model="form.nannprem" class="number-input-text" :precision="2" :min="-9999999999"
                               :max="999999999.99" :controls="false"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="寿险保额" prop="sumins">
              <el-input-number v-model="form.sumins" class="number-input-text" :precision="2" :min="-9999999999"
                               :max="999999999.99" :controls="false"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="Branch" prop="branch">
              <el-input clearable v-model="form.branch" placeholder="请输入" maxlength='2'
                        :readonly="form.method == 'modify' ? true : false"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="saveVipInfo" class="Defaultbutton">保 存</el-button>
        <el-button @click="showVIpDialog = false">返 回</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {mapState, mapGetters} from 'vuex'
  import constant from '@/utils/constant';
  import * as api from "@/api/setting/maintain/vipMaintenance.js";

  export default {
    data() {
      var containChinese = (rule, value, callback) => {
        let CHNReg = /[\u4e00-\u9fa5]/;//汉字的正则表达式
        if (value.match(CHNReg)) {
          callback(new Error('不能有汉字'));
        } else {
          callback();
        }
      };
      var isNum = (rule, value, callback) => {
        const max = 999999999.99;
        if (isNaN(value) || value == "" || value > max) {
          callback(new Error('只能为小于或等于999999999.99的数字'));
        } else {
          callback();
        }
      };
      return {
        valueDate: '',
        showVIpDialog: false,//默认隐藏 修改弹框
        // 查询条件
        rules: {
          chdrcoy: [
            {required: true, message: '请输入公司', trigger: 'blur'},
            {validator: containChinese, trigger: 'blur'}
          ],
          nannprem: [
            {required: true, message: '请输入年缴化保费', trigger: 'blur'},
            {validator: isNum, trigger: 'blur'}
          ],
          sumins: [
            {required: true, message: '请输入寿险保额', trigger: 'blur'},
            {validator: isNum, trigger: 'blur'}
          ],
          branch: [
            {required: true, message: '请输入Branch', trigger: 'blur'},
            {validator: containChinese, trigger: 'blur'}
          ],
        },
        searchParams: {
          chdrcoy: '',
        },
        tableData: [],
        loading: false,

        form: {
          method: '',
          chdrcoy: '',
          nannprem: '',
          sumins: '',
          branch: '',
        },
        option: '',
        options: [
          {value: '1', label: '测试1'},
          {value: '2', label: '测试2'}
        ],

        //需要给分页组件传的信息
        paginations: {
          total: 0,        // 总数
          pageIndex: 1,  // 当前位于哪页
          pageSize: 10,   // 1页显示多少条
          pageSizes: [5, 10, 15, 20],  //每页显示多少条
          layout: "total, sizes, prev, pager, next, jumper"   // 翻页属性
        },
        pickerOptions1: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          }
        }
      }
    },
    created() {
//          this.getList();
    },
    mounted() {
      // this.getList();
    },
    methods: {
      // 查询列表
      getList() {
        this.paginations.total = 0;
        this.tableData = [];
        let para = {
          "chdrcoy": this.searchParams.chdrcoy,
          "pageSize": this.paginations.pageSize,
          "pageNumber": this.paginations.pageIndex,
        }

        this.$store.dispatch('loading/CHANGE_LOADING', true)

        api.queryList(para).then(res => {
          this.$store.dispatch('loading/CHANGE_LOADING', false)
          if (res.resultCode == constant.SUCCESS) {
            if (res.data && res.data.list.length) {
              this.paginations.total = res.data.total;
              this.tableData = res.data.list;
            } else {
              this.$message({
                message: '无符合条件任务！',
                type: 'error'
              });
            }
          }

        }).catch(err => {
          this.$store.dispatch('loading/CHANGE_LOADING', false);
        })
      },

      addVipInfo() {
        this.showVIpDialog = true;
        this.form = {};
        this.form.method = 'add';
      },
      modifyVipInfo(row) {
        this.showVIpDialog = true;
        this.form = Object.assign(this.form, row);
        this.form.method = "modify";
      },

      saveVipInfo() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            api.editData(this.form).then(res => {
              this.$store.dispatch('loading/CHANGE_LOADING', false)
              if (res.resultCode == constant.SUCCESS) {
                this.$message.success(res.resultMsg);
                this.showVIpDialog = false;

                this.tableData = [{
                  chdrcoy: this.form.chdrcoy,
                  nannprem: this.form.nannprem,
                  sumins: this.form.sumins,
                  branch: this.form.branch
                }];
              }
            }).catch(err => {
              this.$store.dispatch('loading/CHANGE_LOADING', false);
            })
          }
        });
      },

      deleteVipInfo(row) {
        let params = {
          'chdrcoy': row.chdrcoy,
          'branch': row.branch,
        }
        if (params.chdrcoy == null || params.chdrcoy == "" || params.branch == null || params.branch == "") {
          this.$message.error("请选择一条数据。")
          return;
        }
        this.$confirm('确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          api.deleteData(params).then(res => {
            this.$store.dispatch('loading/CHANGE_LOADING', false)
            if (res.resultCode == constant.SUCCESS) {
              this.$message.success(res.resultMsg);
              this.subGetList();
            }
          }).catch(err => {
            this.$store.dispatch('loading/CHANGE_LOADING', false);
          })
        }).catch(() => {

        });
      },

      isReadonly() {
        return this.method == 'modify' ? true : false;
      },


      // 每页多少条切换
      handleSizeChange(pageSize) {
        this.paginations.pageSize = pageSize;
        this.getList();
      },
      // 上下分页
      handleCurrentChange(page) {
        this.paginations.pageIndex = page;
        this.getList();
      },
      // 根据条件查询列表
      subGetList() {
        this.paginations.pageIndex = 1;
        this.getList();
      },
      // 清除查询条件
      resetSearch() {
        this.searchParams.chdrcoy = '';
      }
    },
    computed: {
      ...mapGetters(['tags']),
      reload() {
        return this.$store.state.loading.reload;
      }
    },
    activated() {
      if (this.reload) {
        this.$store.dispatch('loading/CHNAGE_RELOAD', false)
        Object.assign(this.$data, this.$options.data());
      }
    }
  }
</script>

<style lang="less" scoped>
  // 通过/deep/可以修改组件内部样式
  .number-input-text /deep/ .el-input__inner {
    text-align: left;
  }

  .fillcontain {
    padding-bottom: 0;
  }

  .contain {
    background: #fff;
    padding: 10px;
    margin-bottom: 20px;
  }

  .pagination {
    padding: 10px 20px;
    text-align: right;
  }

  .operationIcon {
    // padding: 12px;
    border: 1px solid #e5e5e5;
    border-radius: 50%;
    width: 22px;
    height: 22px;
    display: inline-block;
    cursor: pointer;
    box-shadow: 0 0 3px #f9f9f9;
  }

  .search_container {
    padding: 20px 20px 0 20px;
    //    border-top: 1px solid #e1e1e1;
  }

  .common-box {
    padding-bottom: 15px;
  }

  .common-lab {
    line-height: normal !important;
    text-align: right;

    .labelName {
      text-align: right;
    }
  }
</style>



